﻿/*
Copyright (C) 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

var OPTIONS_IDS_KEY = 'ids';

//fullticker includes exchange if needed
function SecurityInfo(fullticker, ticker, support, resistance)
{
  this.fullticker = fullticker;
	this.ticker = ticker;
  this.support = support;
  this.resistance = resistance;
  
	this.lastPctOffSupport = 99;
  this.lastPctOffResistance = 99;

	//only warn once per threshhold per day
	this.lastWarnDay = 0;
	this.lastWarnPct = 99;

}

SecurityInfo.prototype.MinPercentOff = function()
{
	return Math.min(this.lastPctOffSupport, this.lastPctOffResistance)
}
SecurityInfo.prototype.PercentOffSupport = function(price)
{
    var diff = this.resistance - this.support;

    var fromSupport = price - this.support;

    var r = fromSupport / diff;
    
    r =  Math.round(r * 1000) / 10;

        this.lastPctOffSupport = r;
        
	return r;
}

SecurityInfo.prototype.PercentOffResistance = function(price) {
        var diff = this.resistance - this.support;

        var fromReistance = this.resistance - price;

        var r = fromReistance / diff;

//		debug.trace(this.ToString() + "   price: " + price + "  diff: " + diff + " fromR: " + fromReistance +  "  r: " + r);

        r = Math.round(r * 1000) / 10;

		this.lastPctOffResistance = r;

			return r;


}

SecurityInfo.prototype.ToString = function()
{
		return this.fullticker + ',' + this.ticker + ',' + this.support + ',' + this.resistance;
}

SecurityInfo.FromString = function(s, oldInfo) {
    var parts = s.split(',');

    //support for indexes :(
    if (parts.length == 1) {

        parts[1] = parts[0];
        parts[2] = 0;
        parts[3] = 0;
    }

    var si = new SecurityInfo(parts[0], parts[1], parseFloat((parts[2] + '').replace(",", "")), parseFloat((parts[3] + '').replace(",", "")));

    if (oldInfo) {
        si.lastWarnPct = oldInfo.lastWarnPct;
        si.lastWarnDay = oldInfo.lastWarnDay;
    }

    return si;
}


function Security() {
  this.exchange = '';
  this.ticker = '';
  this.last = 0;
  this.change = 0;
  this.changePercent = 0;
  this.lastDateTime = '';
  this.extendedLast = 0;
  this.extendedLastDateTime = '';
  this.extendedChange = 0;
  this.extendedChangePercent = 0;
  this.open = 0;
  this.high = 0;
  this.low = 0;
  this.volume = 0;
  this.averageVolume = 0;
  this.fiftyTwoWeekHigh = 0;
  this.fiftyTwoWeekLow = 0;
  this.marketCap = 0;
  this.name = '';
}

Security.DOW_TICKER = '.DJI';
Security.SP_TICKER = '.INX';
Security.NASDAQ_TICKER = '.IXIC';
Security.NYSE_TICKER = '.NYA';

Security.prototype.getDisplayName = function() {
  if (this.ticker == Security.DOW_TICKER) {
    return strings.DOW;
  } else if (this.ticker == Security.SP_TICKER) {
    return strings.SP;
  } else if (this.ticker == Security.NASDAQ_TICKER) {
    return strings.NASDAQ;
  } else if (this.ticker == Security.NYSE_TICKER) {
    return strings.NYSE;
  }

  return this.ticker;
};

Security.unserializeIds = function(data) {


	debug.trace('unserializing ' + data);
	
	var infos = [];
  
	foreach(x in data.split('|'))
	{
			infos.push(SecurityInfo.FromString(x));
	}

	return infos; //data.split(',');
};

Security.serializeIds = function(idInfos) {

	var s = '';
	for(i = 0; i < idInfos.length; i++)
	{
			if (s != '')
			{
				s += '|';
			}

			s += idInfos[i].ToString();
	}


	debug.trace('Serialized to: ' + s);
  return s;		//ids.join(',');
};

Security.prototype.load = function(data) {
  this.exchange = data.e;
  this.ticker = data.t;
  this.last = data.l;
  this.change = data.c;
  this.changePercent = data.cp;
  this.exchange = data.e;
  this.lastDateTime = data.lt;
  this.extendedLast = data.el;
  this.extendedLastDateTime = data.elt;
  this.extendedChange = data.ec;
  this.extendedChangePercent = data.ecp;
  this.open = data.op;
  this.high = data.hi;
  this.low = data.lo;
  this.volume = data.vo;
  this.averageVolume = data.avvo;
  this.fiftyTwoWeekHigh = data.hi52;
  this.fiftyTwoWeekLow = data.lo52;
  this.marketCap = data.mc;
  this.name = data.name;
};

Security.prototype.getId = function() {
  if (this.exchange) {
    return this.exchange + ':' + this.ticker;
  }

  return this.ticker;
};

Security.EXCHANGE_INDEX_PREFIX = 'INDEX';

Security.prototype.isIndex = function() {
  return this.exchange.substring(0, 5) == Security.EXCHANGE_INDEX_PREFIX;
};

Security.LARGE_CHANGE_THRESHOLD = 2;
Security.INDEX_LARGE_CHANGE_THRESHOLD = 1;

Security.prototype.isLargeMove = function() {
  var threshold = Security.LARGE_CHANGE_THRESHOLD;

  if (this.isIndex() || this.ticker == 'QQQQ') {
    threshold = Security.INDEX_LARGE_CHANGE_THRESHOLD;
  }

  return this.changePercent <= -threshold || this.changePercent >= threshold;
};

Security.prototype.isExtendedLargeMove = function() {
  var threshold = Security.LARGE_CHANGE_THRESHOLD;

  if (this.isIndex() || this.ticker == 'QQQQ') {
    threshold = Security.INDEX_LARGE_CHANGE_THRESHOLD;
  }

  return this.extendedChangePercent <= -threshold || this.extendedChangePercent >= threshold;
};

Security.prototype.isAfterHours = function() {
  return this.extendedLast && this.extendedLast !== 0;
};

Security.prototype.getDelayedMessage = function() {
  var message;

  if (this.isIndex()) {
    message = strings.DATA_REAL_TIME;
  } else if (this.exchange == 'NASDAQ' || this.exchange == 'NYSE') {
    message = Util.substitutePH(strings.EXCH_DATA_REAL_TIME,
        '[![EXCHANGE]!]',
        this.exchange);
  } else if (this.exchange== 'TSE') {
    message = Util.substitutePH(strings.DATA_15_MIN,
        '[![EXCHANGE]!]',
        this.exchange);
  } else if (this.exchange== 'AMEX') {
    message = Util.substitutePH(strings.DATA_20_MIN,
        '[![EXCHANGE]!]',
        this.exchange);
  } else if (!this.exchange) {
    message = strings.DATA_UNKNOWN_MIN;
  } else {
    message = Util.substitutePH(strings.EXCH_DATA_UNKNOWN_MIN,
        '[![EXCHANGE]!]',
        this.exchange);
  }

  return message;
};

Security.prototype.getPrettyExchange = function() {
  return this.isIndex() ? strings.N_A : this.exchange;
};

